package abaco.symbol.variable;

import java.util.Collection;
import java.util.Iterator;

import abaco.Symbol;
import abaco.gui.Panel;
import abaco.symbol.Variable;

public class Set extends Variable {
	protected static final String BASENAME = "variable-set-";

	@Override
	public Symbol buildPanel(Panel panel) {
		return this;
	}

	/**
	 * Allow unnamed child. Actually it adds child using a random name.
	 */
	public Set setSymbolChild(Symbol child) {
		Integer i = children.size();
		String name;
		do {
			name = BASENAME + String.valueOf(i);
			i++;
		} while (children.containsKey(name));
		setSymbolChild(child, name);
		return this;
	}

	@Override
	public String toLatex() {
		if (0 == children.size()) {
			return "{\\emptyset}";
		}
		StringBuilder latex = new StringBuilder();
		latex.append("{\\left\\{");
		Collection<Symbol> list = children.values();
		for (Iterator<Symbol> i = list.iterator(); i.hasNext();) {
			latex.append(i.next().toLatex());
			if (i.hasNext()) {
				latex.append(",");
			}
		}
		latex.append("\\right\\}}");
		return latex.toString();
	}
}
